#include "calc.h"

int PrintMatchPairs(STACK *stack, char *expression)
{
     int i = 0, ret;
     char tmp;
     while (expression[i] != '#')
     {
          if (expression[i] == '(')
               stack_push(stack, &expression[i]);
          else if (expression[i] == ')')
          {
               ret = stack_pop(stack, &tmp);
               if (ret == -1)
                    return -1;
          }
          i++;
     }
     ret = stack_top(stack, &tmp);
     if (ret != -1)
          return -1;
     return 0;
}

int Calculator(STACK *stack, char *expression)
{
     int i = 0;
     double value1, value2, value;
     int ret;

     while (expression[i] != '\0')
     {
          if (expression[i] >= '0' && expression[i] <= '9')
          {
               double num = expression[i] - '0';
               stack_push(stack, &num);
          }
          else
          {
               ret = GetOperands(stack,&value1,&value2);
               switch (expression[i])
               {
               case '+':
                    value = value1 + value2;
                    stack_push(stack, &value);
                    break;
               case '-':
                    value = value1 - value2;
                    stack_push(stack, &value);
                    break;
               case '*':
                    value = value1 * value2;
                    stack_push(stack, &value);
                    break;
               case '/':
                    value = value1 / value2;
                    stack_push(stack, &value);
                    break;
               }
          }
          i++;
     }
     stack_pop(stack, &value);
     printf("value = %.2f\n", value);
}

int GetOperands(STACK *stack, double *left, double *right)
{
     int ret;
     ret = stack_pop(stack, right);
     if (ret == -1)
          return -1;
     ret = stack_pop(stack, left);
     if (ret == -1)
          return -1;
     return 0;
}

void postfix(STACK *stack,char *expression,char * fix_exp)
{
     char tmp = '#';
     stack_push(stack, &tmp);
     int i = 0,j = 0;
     while (!stack_top(stack, &tmp))
     {
          if (expression[i] >= '0' && expression[i] <= '9')
               fix_exp[j++] = expression[i++];
          else
          {
               stack_top(stack, &tmp);
               int pri1 = GetIcp(&expression[i]);
               int pri2 = GetIsp(&tmp);
               if (GetIcp(&expression[i]) > GetIsp(&tmp))
                    stack_push(stack, &expression[i++]);
               else if (GetIcp(&expression[i]) < GetIsp(&tmp))
               {
                    stack_pop(stack, &tmp);
                    fix_exp[j++] = tmp;
               }
               else 
               {
                    stack_pop(stack, &tmp);
                    if (tmp == '(')
                         i++;
                    else
                         return;
               }
          }
     }
}

int GetIsp(char *exp)
{
     switch (*exp)
     {
     case '#':
          return 0;
     case '(':
          return 1;
     case '*':
     case '/':
     case '%':
          return 5;
     case '+':
     case '-':
          return 3;
     case ')':
          return 6;

     default:
          return -1;
     }
}

int GetIcp(char *exp)
{
     switch (*exp)
     {
     case '#':
          return 0;
     case '(':
          return 6;
     case '*':
     case '/':
     case '%':
          return 4;
     case '+':
     case '-':
          return 2;
     case ')':
          return 1;

     default:
          return -1;
     }
}